/**
 * 
 */
cc.Class({
    extends: cc.Component,
    statics: {
        instance: null
    },
    properties: {
        // levelListUI:require("LevelListUI"),
        // loginUI:require("LoginUI"),
        listPrefab:[cc.Prefab],
        listUI:[cc.Node],
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        for(var i = 0; i < this.listPrefab.length; i++){
            if(this.listPrefab[i])
                cc.log(this.listPrefab[i].name)
        }
    },
    showUI(ui_name){
        var ui = this.hasUI(ui_name);
        if(ui){
            // this.hideAll();
            ui.active = true;
            ui.parent = null;
            ui.parent = this.node;
            return ui;
        }
        var prefab = this.getPrefabByName(ui_name);
        if(prefab){
            var ui = cc.instantiate(prefab);
            ui.parent = this.node;
            ui.active = true;
            this.listUI.push(ui);
            return ui;
        }else {
            cc.log("have not prefab: ", ui_name);
        }
    },
    hideUI(ui_name){
        for(var i = 0; i < this.listUI.length; i++){
            if(this.listUI[i].name == ui_name){
                this.listUI[i].active = false;
                return;
            }
        }
    },
    hasUI(ui_name){
        for(var i = 0; i < this.listUI.length; i++){
            if(this.listUI[i] && this.listUI[i].name == ui_name){
                return this.listUI[i];
            }
        }
        return null;
    },
    addUI(uiNode){ //将已经创建好的UI 放进容器
        if(!uiNode)
            return;
        if(!this.hasUI(uiNode.name)){
            this.listUI.push(uiNode);
        }
    },
    getUI(ui_name){
        for(var i = 0; i < this.listUI.length; i++){
            if(this.listUI[i].name == ui_name){
                return this.listUI[i];
            }
        }
        return null;
    },
    getUICom(ui_name){  //获取组件
        for(var i = 0; i < this.listUI.length; i++){
            if(this.listUI[i].name == ui_name){
                return this.listUI[i].getComponent(ui_name);
            }
        }
        return null;
    },
    hideAll(){
        for(var i = 0; i < this.listUI.length; i++){
            this.listUI[i].active = false;
        }
    },
    getPrefabByName(prefab_name){
        for(var i = 0; i < this.listPrefab.length; i++){
            if(this.listPrefab[i] && this.listPrefab[i].name == prefab_name){
                return this.listPrefab[i];
            }
        }
        return null;
    },
    // update (dt) {},
    showTip (tip, okCB) {
        var obj = this.showUI("TipUI");
        if(obj){
            var com = obj.getComponent("TipUI");
            com.setTip(tip, okCB);
        }
    },
    showTipOKCancel (tip, okCB, cancelCB, okStr, cancelStr) {
        var obj = this.showUI("TipUI");
        if(obj){
            var com = obj.getComponent("TipUI");
            com.setTipOKCancel(tip, okCB, cancelCB, okStr, cancelStr);
        }
    },
});
